const express = require('express');
const cors = require('cors');
const authRoutes = require('./routes/authRoutes');  // 导入登录路由
const bookRoutes = require('./routes/bookRoutes');  // 导入书籍路由
const navigationRoutes = require('./routes/navigationRoutes');  // 导入导航栏路由
const menuRoutes = require('./routes/menuRoutes');            // 侧边栏菜单
const userRoutes = require('./routes/userRoutes');  //用户信息
const { swaggerUi, swaggerDocs } = require('./swagger/swagger'); // 导入 Swagger 配置

const app = express();
const port = 3000;

// 启用跨域
app.use(cors());

// 解析 JSON 和 URL-encoded 数据
app.use(express.json());
app.use(express.urlencoded({extended: true}));

// 静态资源托管
app.use('/uploads', express.static('uploads'));

// Swagger 路由
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs));  // 将 Swagger UI 添加到 '/api-docs' 路径

// 使用书籍路由
app.use('/BookshelfManagement', bookRoutes);

// 使用导航栏路由
app.use('/navigation', navigationRoutes);

// 使用认证路由（登录）
app.use('/auth', authRoutes);

app.use('/api', menuRoutes);

// 用户路由
app.use('/api', userRoutes);

// 启动服务器
// app.listen(port, () => {
//     console.log(`Server is running on http://localhost:${port}`);
// });

// 绑定到 0.0.0.0，允许外部设备访问
app.listen(port, '0.0.0.0', () => {
    console.log(`Server is running on http://0.0.0.0:${port}`);
});
